package com.cedarhd.brzx.domain.annotation;


import org.hibernate.annotations.common.util.StringHelper;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by lipei on 2018/5/7.
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotEmptyXs.Validator.class)
public @interface NotEmptyXs {

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String message() default "invalid input";

    public class Validator implements ConstraintValidator<NotEmptyXs, String> {

        @Override
        public void initialize(NotEmptyXs notEmptyDemo) {}

        @Override
        public boolean isValid(String arg0, ConstraintValidatorContext context) {
            if(arg0 == null || StringHelper.isEmpty(arg0) || arg0.trim().length() == 0){
                return false;
            }

            return true;
        }
    }
}
